package com.example.wan_android.adapter

import android.widget.ImageView
import com.bumptech.glide.Glide
import com.example.wan_android.BR
import com.example.wan_android.R
import com.example.wan_android.core.CoreViewHolder
import com.example.wan_android.databinding.AdapterKnowledgeBodyBinding
import com.example.wan_android.http.bean.KnowledgeBodyBeansData
import com.example.wan_android.viewmodel.KnowledgeBodyViewModel

class KnowledgeBodyAdapter(vm: KnowledgeBodyViewModel) :
    CoreAdapter<AdapterKnowledgeBodyBinding, KnowledgeBodyViewModel, KnowledgeBodyBeansData>(
        BR.data,
        BR.viewModel,
        vm
    ) {
    override fun getItemLayoutId(viewType: Int): Int {
        return R.layout.adapter_knowledge_body
    }

    override fun onBindViewHolder(holder: CoreViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        if (mListData[position].collect) {
            Glide.with(holder.itemView).load(R.drawable.ic_collected)
                .into(holder.binding.root.findViewById(R.id.iv_love))
            holder.binding.root.findViewById<ImageView>(R.id.iv_love).setOnClickListener({
                mViewModel?.unCollection(mListData[position].id)
                mListData[position].collect = false
                notifyItemChanged(position)
            })
        } else {
            Glide.with(holder.itemView).load(R.drawable.ic_collect)
                .into(holder.binding.root.findViewById(R.id.iv_love))
            holder.binding.root.findViewById<ImageView>(R.id.iv_love).setOnClickListener({
                mViewModel?.collection(mListData[position].id)
                mListData[position].collect = true
                notifyItemChanged(position)
            })
        }
    }
}